// Doc/Reference/Certainties.md
#if !(UNITY_EDITOR || DEBUG)
#define AL_OPTIMIZE
#endif

using System;
using InvOp = System.InvalidOperationException;
namespace Active.Core
{
    public readonly partial struct failure
    {

        public static failure[] values = { _fail };

        [Obsolete("Use _fail instead", true)]
        internal static readonly failure _flop = new failure();
        internal static readonly failure _fail = new failure();

        // --------------------------------------------------------------

        public static failure operator %(failure x, failure y) => _fail;
        public static failure operator |(failure x, failure y) => y;

        public static failure operator &(failure x, status y)
        => throw new InvOp($"({x} & {y}) is not allowed");

        public static loop operator +(failure x) => loop._cont;

        public static bool operator true(failure s) => false;
        public static bool operator false(failure s)
        => throw new InvOp("falsehood cannot be tested (failure)");

        public override bool Equals(object x) => x is failure;

        override public int GetHashCode() => -1;

        public static implicit operator bool(failure self) => false;

#if AL_OPTIMIZE   // --------------------------------------------

    public static failure fail(ValidString reason = null) => _fail;

    public static action operator ! (failure s) => action._done;

    public static implicit operator impending(failure self)
    => impending._fail;

    public static implicit operator status(failure self)
    => status._fail;

#endif

    }
}
